package programmercarl.动态规划.C23;

class Solution {
    public int coinChange(int[] coins, int amount) {

        //最少数量
        int[] dp = new int[amount + 1];
        //初始化
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) {
            dp[i] = Integer.MAX_VALUE;
        }
        //求组合，先物品
        for (int i =0 ;i < coins.length;i++) {
            for (int j = coins[i]; j <= amount; j++) {//这里以coin[i]是为了够扣，不然都放不下，没必要遍历
                //只有dp[j-coins[i]]不是初始最大值时，该位才有选择的必要
                if (dp[j - coins[i]]!=Integer.MAX_VALUE) {//如果是
                    dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
                }
            }
        }
        //完全背包
        return dp[amount]==Integer.MAX_VALUE?-1:dp[amount];
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int i = solution.coinChange(new int[]{1, 2, 5}, 11);
        System.out.print(i);
    }
}